<?php

namespace XWAM\Addon\PXCallSystem\Controller;

/**
 * 语音菜单控制器
 *
 * @author Alen
 * @since 1.0 2014-7-2 by sutroon
 */
class MusicMenuController extends AppbaseController {

    public function index(){
        M()->execute("update tcti_voice_menu set music='' where outOper in (0,7)");
        $this->display();
    }
    public function get_musicmenu_search_data(){
        $search=I();
        if (!$this->isSuperAdmin) {
            $where = 'tcti_voice_menu.siteID=' . $this->loginData['siteID'];
        }
        if($search['siteID']){
            $where='tcti_voice_menu.siteID=' . $search['siteID'];
        }
        return array('where'=>$where);
    }
    /**
     * 显示项目列表
     */
    public function get_musicmenu_list_json() {
        $search_data=$this->get_musicmenu_search_data();
        $list=D('MusicMenu')->get_musicmenu_list($search_data['where']);
        $this->ajaxReturn($list);
    }

    public function get_json() {
        if ($this->isSuperAdmin) {
            $where['siteID'] = I('siteID');
        } else {
            $where['siteID'] = $this->loginData['siteID'];
        }
        $data = M('tcti_voice_menu')->cache(true)->where($where)->field('id, id as text')->select();
        $this->ajaxReturn($data);
    }

    /**
     * 显示项目编辑表单
     */
    public function item_edit($id=0) {
        if ($id && is_numeric($id)) {
            $M = M('tcti_voice_menu');
            $where['id'] = I('id');
            $list = $M->where($where)->select();
            $this->assign('data', $list[0]);
        } else {
            $this->assign('data', null);
        }
        $this->display();
    }

    /**
     * 保存项目
     */
    public function item_save() {
        $params = new \Org\Su\Params();
        $params->addParam('siteID', 'number', '租户未选择!');
        $params->addParam('outOper', 'require', '呼通后操作必须填写!');

        $outOper = I('outOper');
        if (strpos(',1,6,2,3,4,5,', ",$outOper,") !== false) {
            $params->addParam('music', 'require', '放音文件必须选择!');
        } else {
            $params->addData('music', '');
        }
        if (strpos(',1,2,3,4,5,', ",$outOper,") !== false) {
            $params->addParam('musicLimit', 'number', '最大放音次数必须填写!');
        } else {
            $params->addData('musicLimit', '');
        }
        if (strpos(',1,2,', ",$outOper,") !== false) {
            $params->addParam('outParam', 'require', '呼通后操作具体参数必须填写!');
        } else {
            $params->addData('outParam', '');
        }
        if (strpos(',0,6,', ",$outOper,") !== false) {
            $params->addParam('outQueue', 'require', '呼通后操作队列号必须选择!');
        } else {
            $params->addData('outQueue', '');
        }
        if (strpos(',0,1,6,', ",$outOper,") !== false) {
            $outEcEnable = I('outEcEnable');
            //$params->addParam('sayExtension', 'number', '播放工号标识必须选择!');
        } else {
            $outEcEnable = 0;
            //$params->addData('sayExtension', '0');
        }
        $params->addData('outEcEnable', $outEcEnable);
        if ($outEcEnable == '1') {
            $params->addParam('outEcMusic', 'require', '评价放音文件必须选择!');
            //$params->addParam('outEcKey', 'require', '评价按键必须填写!');
        } else {
            $params->addData('outEcMusic', '');
            //$params->addData('outEcKey', '');
        }
        $params->addParam('remark', 'require', '', true);
        if ($params->hasError()) {
            $this->ajaxMsg(false, $params->getError());
        }
        $id = I('id');
        $M = M('tcti_voice_menu');
        if ($id) {
            $where['id'] = $id;
            $result = $M->where($where)->save($params->getParams());
        } else {
            $params->addData('createdTime', date('Y-m-d H:i:s'));
            $result = $M->add($params->getParams());
            $id = $result;
        }
        $this->ajaxMsg($result, '保存{%}!');
    }

    /**
     * 删除项目
     */
    public function item_delete($id='') {
        if (!$id){
            $this->ajaxMsg(false, '编号丢失!');
        }
        $result = M('tcti_voice_menu')->delete($id);
        $this->force_need_reload();
        $this->ajaxMsg($result, '删除{%}');
    }

}
